﻿using System;
using System.Text.RegularExpressions;

namespace Core
{
    public struct PathParser
    {
        public string DirectoryName { get; private set; }
        public string FileName { get; private set; }
        public string Extension { get; private set; }
        public string Name { get; private set; }
        public string FolderName { get; private set; }

        public PathParser(string path)
        {
            var start_idx = path.LastIndexOf('/');
            var end_idx = path.LastIndexOf(".", StringComparison.Ordinal);

            DirectoryName = path[..start_idx];
            FileName = path[(start_idx + 1)..];
            Extension = path[(end_idx + 1)..];
            Name = path[(start_idx + 1)..end_idx];
            FolderName = DirectoryName[DirectoryName.LastIndexOf('/')..];
        }

        public readonly string GetProjName()
        {
            var input = DirectoryName;
            var pattern = @"/([gs]_.+?)/";

            var name = string.Empty;
            foreach (Match match in Regex.Matches(input, pattern))
            {
                if (match.Groups.Count > 1)
                {
                    name = match.Groups[1].Value;
                    break;
                }
            }
            return name;
        }
    }
}
